package com.example.demo.lc;

/**
 * @Description TODO
 * @Author wzh
 * @Date 2021/12/23 7:17 下午
 * @Version 1.0
 */
public class Leetcode443 {
    public static void main(String[] args) {
        //char[] chars = {'a'};
        char[] chars = {'a','a','b','b','c','c','c'};
        //char[] chars = {'a','b','b','b','b','b','b','b','b','b','b','b','b'};
        System.out.println(compress(chars));
    }

    public static int compress(char[] chars) {
        int cnt = 0;
        int i = 0;
        char tmp = '0';
        for(char ch: chars){
            if(tmp == ch){
                ++cnt;
            }else{
                if(cnt >1){
                    for (char c : String.valueOf(cnt).toCharArray()) {
                        chars[i++] = c;
                    }
                }
                cnt = 1;
                tmp = ch;
                chars[i++] = ch;
            }
        }
        if(cnt >1){
            for (char c : String.valueOf(cnt).toCharArray()) {
                chars[i++] = c;
            }
        }
        char[] newchar = new char[i];
        System.arraycopy(chars,0,newchar,0,i);
        return i;
    }

}
